﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace Common
{
    public class GuidHelper
    {
        /// <summary>
        /// 生成唯一字符
        /// </summary>
        /// <returns></returns>
        public static string stringId
        {
            get
            {
                long i = 1;
                foreach (byte b in Guid.NewGuid().ToByteArray())
                {
                    i *= ((int)b + 1);
                }
                return string.Format("{0:x}", i - DateTime.Now.Ticks);
            }
        }

        /// <summary>
        /// 生成唯一数字
        /// </summary>
        /// <returns></returns>
        public static long longId
        {
            get
            {
                byte[] buffer = Guid.NewGuid().ToByteArray();
                return BitConverter.ToInt64(buffer, 0);
            }
        }

        /// <summary>
        /// 生成唯一MD5加密字符
        /// </summary>
        public static string stringMd5Id
        {
            get
            {
                var str = Guid.NewGuid().ToString();
                return EncryptionHelper.MD5Helper.Encryt(str);
            }
        }

        /// <summary>
        /// 生成消息加/解密密钥（长度为43由字母数字组成的文字）
        /// </summary>
        public static string stringAESKey
        {
            get
            {
                var key = "";
                var random = new Random((int)DateTime.Now.Ticks);
                string str = @"0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ";

                for (var i = 0; i < 43; i++)
                {
                    key = key + str.Substring(random.Next(str.Length - 1), 1);
                }

                return key;
            }
        }
    }
}
